import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import homePage from "@/views/home/homePage.vue";
import detailsPage from "@/views/home/detailsPage.vue";
import loginPage from "@/views/home/components/loginPage.vue";
import personalCenter from "@/views/home/personalCenter.vue";
import {viewLogin} from "@/API/http";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: homePage
  },

  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
  // 首页
    path: '/',
    // component: homePage、
  },
  {
    // 详情页
    path: '/details',
    component: detailsPage
  },
  {
    // 登录
    path: '/login',
    component: loginPage
  },
  {
    // 个人中心
    path: '/personal',
    component: personalCenter
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  next()
  if(to.path === '/personal') {
    viewLogin().then(res=>{
      if(res.code===0){
        next()
      }else {
        next('/')
      }
    })
  }else {
    next()
  }
})
export default router
